package com.glodon.paas.document.dao.mysql;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.document.dao.ProjectMemberDao;
import com.glodon.paas.document.dao.domain.InvitationEntity;
import com.glodon.paas.document.dao.domain.ProjectMemberEntity;
import com.glodon.paas.document.dao.mapper.InvitationMapper;
import com.glodon.paas.document.dao.mapper.ProjectMemberAttendeesMapper;
import com.glodon.paas.document.dao.mapper.ProjectMemberMapper;
import com.glodon.paas.util.DateUtil;

public class ProjectMemberDaoMysqlImpl implements ProjectMemberDao {

    @Autowired
    private ProjectMemberMapper projectMemberMapper;
    @Autowired
    private InvitationMapper    invitationMapper;
    @Autowired
    private ProjectMemberAttendeesMapper projectMemberAttendeesMapper;
    @Override
    public String insert(ProjectMemberEntity entity) {
        projectMemberMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public ProjectMemberEntity get(String id) {
        return projectMemberMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(String id) {
        return projectMemberMapper.removeMember(id);
    }

    @Override
    public List<ProjectMemberEntity> getMembers(String projectId, String roleId) {
        return projectMemberMapper.selectAllMembers(projectId, roleId);
    }

    @Override
    public boolean acceptMember(String userId, String token) {
        InvitationEntity entity = invitationMapper.getInvitation(userId, token);
        if (entity != null) {
            int result = projectMemberAttendeesMapper.updateStatus(userId,entity.getMemberId(),1);
            if(result>0){
                result=projectMemberMapper.acceptMember(entity.getMemberId(),DateUtil.getNow()) ;
            }
            return result> 0;
        }
        return false;
    }

    @Override
    public boolean rejectMember(String id) {
        return projectMemberMapper.rejectMember(id) > 0;
    }

    @Override
    public boolean updateMemberStatus(String id, int status) {
        return projectMemberMapper.updateMemberStatus(id, status) > 0;
    }

    @Override
    public int updateDefaultRole(String projectId, String roleId) {
        return projectMemberMapper.updateDefaultRole(projectId, roleId);
    }

    @Override
    public int updateRoleOfMember(String memberId, String projectId, String oldRoleId, String newRoleId) {
        return projectMemberMapper.updateRoleOfMember(memberId, projectId, oldRoleId, newRoleId);
    }

    @Override
    public ProjectMemberEntity getMember(String projectId, String roleId, String accountId,String orgId) {
        return projectMemberMapper.getSingleMember(projectId,roleId,accountId,orgId);
    }

    @Override
    public List<ProjectMemberEntity> getAddedOrgs(String projectId, String roleId) {
        return projectMemberMapper.getAddedOrgs(projectId,roleId);
    }
    
    public void setProjectMemberMapper(ProjectMemberMapper projectMemberMapper) {
        this.projectMemberMapper = projectMemberMapper;
    }
    
    public void setInvitationMapper(InvitationMapper invitationMapper) {
        this.invitationMapper = invitationMapper;
    }
}
